<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Log;
// use Dcat\Admin\Widgets\Form;
class EquipmentExceptionRecordController extends Controller
{
    //电除尘改造信息调查表
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$this->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('设备异常记录汇总表')
            ->description('分步表单');
    }
    protected function form()
{
    Admin::js('/stepform/js/jquery.smartWizard.min.js');
    Admin::css('/stepform/css/step.css');
    return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('设备异常记录汇总表');
            $form->action('EquipmentExceptionRecordstep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember()
                ->width('100%')
                ->add('', function (StepForm $step) use ($form) {
                    $step->table('jlhz', trans('记录汇总'),function ($table){
                        $table->row(function($table){
                            $table->text('组成编号', '编号');
                            $table->text('设备名称','设备名称');
                            $table->text('部位','部位');
                            $table->text('异常现象','异常现象');
                            $table->text('检查人','检查人');
                        });
                        $table->row(function($table){
                            $table->text('检查时间','检查时间');
                            $table->text('处理情况','处理情况');
                            $table->text('处理时间','处理时间');
                            $table->text('处理人','处理人');
                            $table->radio('停机检修','停机检修')->options(
                                ['是' => '是','否' => '否']
                            )->default('是');
                            $table->text('部门','部门');
                        });
                    })->width(12,0);
                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            Log::info($info);
            //实例化 phpword 类
            $PHPWord = new PhpWord();
            //指定事先制作好的模板文件路径
            $templateProcessor = new TemplateProcessor(public_path("filemodel/eer.docx"));

            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            
            if(isset($info['jlhz'])){
                $row=count($info['jlhz']);//总行数
                $templateProcessor->cloneRow('序号',$row);//复制行
                $pa = $info['jlhz'];
                $pa = array_values($pa);
                for($d=0;$d<$row;$d++){
                    $templateProcessor->setValue("序号#".($d+1),$d+1);
                    $templateProcessor->setValue("组成编号#".($d+1),$pa[$d]['组成编号']);
                    $templateProcessor->setValue("设备名称#".($d+1),$pa[$d]['设备名称']);
                    $templateProcessor->setValue("部位#".($d+1),$pa[$d]['部位']);
                    $templateProcessor->setValue("异常现象#".($d+1),$pa[$d]['异常现象']);
                    $templateProcessor->setValue("检查人#".($d+1),$pa[$d]['检查人']);
                    $templateProcessor->setValue("检查时间#".($d+1),$pa[$d]['检查时间']);
                    $templateProcessor->setValue("处理情况#".($d+1),$pa[$d]['处理情况']);
                    $templateProcessor->setValue("处理时间#".($d+1),$pa[$d]['处理时间']);
                    $templateProcessor->setValue("处理人#".($d+1),$pa[$d]['处理人']);
                    $templateProcessor->setValue("停机检修#".($d+1),$pa[$d]['停机检修']);
                    $templateProcessor->setValue("部门#".($d+1),$pa[$d]['部门']);
                }
            }
            
            
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = '设备异常记录汇总表'.date('Y-m-d H:i');
            $data['data'] = $info;
            $insertData['name'] = $data['name'];
            $insertData['type'] = 10;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            $templateProcessor->saveAs(public_path('filemodellist/'.$data['name'].".docx"));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }

    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }

    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');;
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }
}